//
//  TF1to16ExpressionView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/6.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF1to16ExpressionView: UIView,UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return TalkfunSharedTools.ExpressionsArray().count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TF1to16ExpressionCell", for: indexPath) as!TF1to16ExpressionCell
        
        let str = TalkfunSharedTools.ExpressionsArray()[indexPath.row] as! String
        cell.imageName = str
        cell.imageButton.setImage(UIImage.init(named: str), for: .normal)
       
        cell.myBlock = { [weak self] (str) in
            guard let `self` = self else { return }
            if (self.expressionBlock != nil) {
                self.expressionBlock!(str)
            }
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        
        let fSpacing = (self.frame.size.height-30-30)/3
        
        //分别为上、左、下、右
        return UIEdgeInsets.init(top: fSpacing, left: 50, bottom: 0, right: 50)
        }
    

   //  ell与左右间距 橫
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        
        let expressionsArray = TalkfunSharedTools.ExpressionsArray()
        return (self.frame.size.width-100)/CGFloat(expressionsArray.count)
        

    }
   
    // cell与上下间距 纵
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        let fSpacing =  (self.frame.size.height-30-30)/3
        return fSpacing;
   
    }
    @objc public let deleteBtn = UIButton()  //删除表情包
    @objc public var expressionBlock:TF1to16ExpressionCellmyBlock?
    @objc public var delExpressionBlock:TF1to16ExpressionCellmyBlock?
    var collectionView:UICollectionView?

    @objc func deleteExpression()
    {
        if (self.delExpressionBlock != nil) {
            self.delExpressionBlock!("")
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if (self.frame.size.width>0&&self.frame.size.height>0) {
            if (self.collectionView==nil) {
                
               
                let layout = UICollectionViewFlowLayout()
                layout.itemSize = CGSize(width: 30, height: 30)
                //垂直方向
                layout.scrollDirection = .vertical
                
                self.collectionView = UICollectionView.init(frame: self.bounds, collectionViewLayout: layout)
                
                self.collectionView!.dataSource=self
                self.collectionView!.delegate=self
                
                //注册Cell，必须要有
                self.collectionView?.register(TF1to16ExpressionCell.self, forCellWithReuseIdentifier: "TF1to16ExpressionCell")
              
                self.addSubview(self.collectionView!)
                
                self.collectionView!.backgroundColor = UIColor.clear
                
                self.deleteBtn.frame = CGRect(x: self.frame.size.width-56-27.5, y: self.frame.size.height-44-26, width: 56, height: 44)
                self.deleteBtn.backgroundColor = UIColor(red: 241/255.0, green: 246/255.0, blue: 249/255.0, alpha: 1)
                self.deleteBtn.addTarget(self, action: #selector(self.deleteExpression), for: .touchUpInside)
                self.deleteBtn.setImage(UIImage.init(named: "HTLive.bundle/del"), for: .normal)
               
                self.addSubview(self.deleteBtn)
                self.deleteBtn.layer.masksToBounds = true
                self.deleteBtn.layer.cornerRadius = 12.0
                
                if #available(iOS 11.0, *) {
                    
                    self.collectionView!.contentInsetAdjustmentBehavior = .never
                    
                }
                

            }else{
                self.collectionView!.frame = self.bounds
            }
            self.collectionView!.reloadData()
          
            
        }
    }

}
